package com.atguigu.gmallpublisher1.controller;

import com.atguigu.gmallpublisher1.service.ProductStatsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Map;

@RestController
@RequestMapping("/api/sugar")
public class SugarController {

    @Autowired
    private ProductStatsService productStatsService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date) {

        if (date == 0) {
            date = getToday();
        }

        //查询ClickHouse数据
        BigDecimal gmv = productStatsService.getGmv(date);

        //封装JSON并返回结果
        return "{ " +
                "  \"status\": 0, " +
                "  \"msg\": \"\", " +
                "  \"data\": " + gmv + " " +
                "}";

    }

    @RequestMapping("/tm")
    public String getGmvByTm(@RequestParam(value = "date", defaultValue = "0") int date,
                             @RequestParam(value = "limit", defaultValue = "5") int limit) {

        //处理日期
        if (date == 0) {
            date = getToday();
        }

        //查询ClickHouse获取品牌GMV数据   :: Map[(苹果->219122.00),(TCL->102888.00)]
        Map gmvByTm = productStatsService.getGmvByTm(date, limit);

        //封装JSON数据并返回结果
        return "            {" +
                "              \"status\": 0," +
                "              \"msg\": \"\"," +
                "              \"data\": {" +
                "                \"categories\": [\"" +
                StringUtils.join(gmvByTm.keySet(), "\",\"") +
                "\"]," +
                "                \"series\": [" +
                "                  {" +
                "                    \"name\": \"GMV\"," +
                "                    \"data\": [" +
                StringUtils.join(gmvByTm.values(), ",") +
                "]" +
                "                  }" +
                "                ]" +
                "              }" +
                "            }";

    }

    private int getToday() {
        long ts = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(ts));
    }

}
